function pad(num, n) 
{
	// return Array(n>num?(n-(''+num).length+1):0).join(0)+num;
	var len = num.toString().length;
    while(len < n) {
        num = "0" + num;
        len++;
    }
    return num;
}

function select_option(sel,value,selIsObject)
{
	var options = null;
	if(selIsObject)
	{
		options = sel.find('option');
	}
	else
	{
		options = $('#' + sel + ' option');
	}
	  
	options.each(function(){
		if($(this).val() == value)
		{
			$(this).attr('selected','selected');
		}
	});
}

$(document).ready(function(){
	$('span[type=datetime]').each(function(){
		var name = $(this).attr('name');
		var value = $(this).attr('value');

		var container = $(this);
		// alert($(this).attr('tagName'));
		container.html('');
		var txt_hid = $("<input name='" + name + "' type='hidden' value='" + value + "' size='12'/>");

		var txt_date = $("<input type='text' style='width:70px;'/>");
		container.append(txt_hid);
		container.append(txt_date);
		container.append(' ');
		var sel_hour = $("<select></select>");
		container.append(sel_hour);
		container.append(':');
		//var $opt = $("<option value='00'>请选择</option>");
		//sel_hour.append($opt);
		for(i=0;i<=23;i++)
		{
			i = pad(i,2);
			var $opt = $("<option value='" + i + "'>" + i + "</option>");
			sel_hour.append($opt);
		}

		var sel_minute = $("<select></select>");
		container.append(sel_minute);
		//var $opt = $("<option value='00'>请选择</option>");
		//sel_minute.append($opt);
		for(i=0;i<=59;i++)
		{
			i = pad(i,2);
			var $opt = $("<option value='" + i + "'>" + i + "</option>");
			sel_minute.append($opt);
		}
		// 绑定重新计算日期的事件
		txt_date.datepicker({
			changeMonth: true,
			changeYear: true,
			dateFormat: "yy-mm-dd",
			onSelect : function(dateText, inst){
				txt_hid.val(txt_date.val()+ " " + sel_hour.val() + ":" + sel_minute.val());
			}
		});

		sel_hour.change(function(){
			txt_hid.val(txt_date.val()+ " " + sel_hour.val() + ":" + sel_minute.val());
		});
		sel_minute.change(function(){
			txt_hid.val(txt_date.val()+ " " + sel_hour.val() + ":" + sel_minute.val());
		});
		
		// 绑定默认值 
		if(value!='')
		{
			date = value.split(' ')[0];
			time = value.split(' ')[1];
			hour = time.split(':')[0];
			minute = time.split(':')[1];
			txt_date.val(date);
			select_option(sel_hour,hour,true);
			select_option(sel_minute,minute,true);
		}
	});
	 
	$('span[type=date]').each(function(){
		var name = $(this).attr('name');
		var value = $(this).attr('value');

		var container = $(this);
		// alert($(this).attr('tagName'));
		container.html('');
		var txt_date = $("<input name='" + name + "' type='text' style='width:76px'/>");
		container.append(txt_date);
		
		// 绑定重新计算日期的事件
		txt_date.datepicker({
			changeMonth: true,
			changeYear: true,
			dateFormat: "yy-mm-dd" 
		});
		 
		// 绑定默认值 
		if(value!='')
		{
			txt_date.val(value);
		}
	});
	
	$('select.bind').each(function(){
		var id = $(this).attr('id');
		var txt_name = "p_" + id; 
		select_option($(this),$("input[name='" + txt_name + "']").val(),true);

		$(this).change(function(){
			$("input[name='" + txt_name + "']").val($(this).val());
		});
	});
});
